﻿//-----------------------------------------------------------------------
// <copyright file="ToolBase.cs" company="Nathan Miller">
// Copyright (c) Nathan Miller.
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
//-----------------------------------------------------------------------
namespace TileTool.Base.Tool
{
    using System;
    using TileTool.Message;
    using TileTool.Tool;

    /// <summary>
    /// Base class for all tools in the base implementation
    /// </summary>
    public abstract class ToolBase : ITool
    {
        #region Constructor - Public

        /// <summary>
        /// Default constructor.
        /// </summary>
        public ToolBase()
        {
            this.Mode = ToolMode.Modal;
        }

        #endregion

        #region Properties - Public

        /// <summary>
        /// Gets or sets the mode that the tool should be run in.
        /// </summary>
        public ToolMode Mode
        {
            get;
            protected set;
        }

        /// <summary>
        /// Gets the tool manager that currently hosts the tool.
        /// </summary>
        public IToolManager Manager
        {
            get;
            protected set;
        }

        #endregion

        #region Methods - Public

        /// <summary>
        /// Called when a tool is installed into a tool manager. Should be called
        /// by the tool manager after the tool is already in the tool stack.
        /// </summary>
        /// <param name="manager">Tool manager that is hosting the tool. Always non-null.</param>
        public virtual void Install(IToolManager manager)
        {
            if (manager == null)
            {
                throw new ArgumentNullException("manager", "Tool manager must be non-null.");
            }

            this.Manager = manager;
        }

        /// <summary>
        /// Called when a tool is uninstalled from a tool manager. Should be called
        /// by the tool manager after the tool has already been removed form the tool
        /// stack.
        /// </summary>
        public virtual void Uninstall()
        {
        }

        #endregion

        #region Methods - Protected

        /// <summary>
        /// Send a message.
        /// </summary>
        /// <param name="msg">Message to send.</param>
        protected virtual void SendMessage(MessageBase msg)
        {
            if (this.Manager == null)
            {
                throw new InvalidOperationException("Manager must be non-null to send a message.");
            }

            MessageRouter router = this.Manager.MessageRouter;

            router.SendMessage(msg);
        }

        #endregion
    }
}
